<template>
  <Layout>
    <div class="volunteer-team">
      <!-- 面包屑导航 -->
      <div class="breadcrumb">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>
            <router-link to="/home" class="breadcrumb-link">首页</router-link>
          </el-breadcrumb-item>
          <el-breadcrumb-item>
            <span class="current-page">志愿队伍</span>
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 筛选区域 -->
      <div class="filter-section">
        <!-- 队伍区域 -->
        <div class="filter-row">
          <div class="filter-label">服务区域</div>
          <div class="filter-options">
          <span 
            v-for="area in areas" 
            :key="area"
            class="option"
            :class="{ 
              'default-active': !selectedFilters.area && area === '全部',
              'active': selectedFilters.area === area
            }"
            @click="handleFilterClick('area', area)"
          >{{ area }}</span>
        </div>
        </div>

        
        <!-- 队伍人数 -->
        <div class="filter-row">
          <div class="filter-label">队伍人数</div>
          <div class="filter-options">
            <span 
              v-for="teamSize in teamSizes" 
              :key="teamSize"
              class="option"
              :class="{ 
                'default-active': !selectedFilters.teamSize && teamSize === '全部',
                'active': selectedFilters.teamSize === teamSize
              }"
              @click="handleFilterClick('teamSize', teamSize)"
            >{{ teamSize }}</span>
          </div>
        </div> 
      </div>

      <!-- 队伍列表 -->
      <div class="team-list">
        <div v-for="team in teams" 
             :key="team.organizationId" 
             class="team-card"
             @click="handleTeamClick(team.organizationId)">
          <div class="team-logo">
            <img :src="team.avatarUrl || '/team-logos/default.png'" :alt="team.organizationName">
          </div>
          <div class="team-info">
            <h3>{{ team.organizationName }}</h3>
            <p class="team-desc">{{ team.organizationProfile }}</p>
            <div class="team-meta">
              <span>成立时间: {{ formatDate(team.establishmentDate) }}</span>
              <span>队伍人数: {{ team.personnelScale }}人</span>
              <span>服务区域: {{ team.region }}</span>
            </div>
            <div class="team-stats">
              <div class="stat-item">
                <div class="stat-value">{{ team.trainingSatisfaction || 0 }}</div>
                <div class="stat-label">培训满意度</div>
              </div>
              <div class="stat-item">
                <div class="stat-value">{{ team.cooperationSatisfaction || 0 }}</div>
                <div class="stat-label">合作满意度</div>
              </div>
              <div class="stat-item">
                <div class="stat-value">{{ team.starRating || '暂无' }}</div>
                <div class="stat-label">评分</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </Layout>
</template>

<script setup>
import Layout from '@/components/Layout.vue'
import { ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'

const router = useRouter()

// 筛选选项数据
const areas = ['全部', '南珠东校区', '南珠西校区', '银滩校区']
const teamSizes = ['全部', '1-20', '21-50', '51-100', '100以上']

// 选中状态
const selectedFilters = ref({
  area: '',
  teamSize: ''
})

// 处理筛选点击
const handleFilterClick = (filterType, value) => {
  selectedFilters.value[filterType] = value === '全部' ? '' : value
  fetchTeams()
}

// 处理队伍点击
const handleTeamClick = (teamId) => {
  router.push(`/team/${teamId}`)
}

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return '暂无'
  const date = new Date(dateStr)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 团队数据
const teams = ref([])

// 获取团队列表
const fetchTeams = async () => {
  try {
    const params = {}
    if (selectedFilters.value.area) {
      params.address = selectedFilters.value.area
    }
    if (selectedFilters.value.teamSize) {
      params.personnelScale = selectedFilters.value.teamSize
    }
    
    const response = await axios.get('http://localhost:9090/organizations/search', {
      params: params
    })
    teams.value = response.data
  } catch (error) {
    console.error('获取团队列表失败:', error)
  }
}

onMounted(() => {
  fetchTeams()
})
</script>

<style scoped>
.volunteer-team {
max-width: 1200px;
margin: 20px auto;
}

/* 面包屑导航样式 */
.breadcrumb {
  margin-bottom: 20px;
  padding: 12px 20px;
  background-color: #f8f8f8;
  border-bottom: 1px solid #eaeaea;
}

.breadcrumb-link {
  color: #dd2b2b;
  text-decoration: none;
}

.current-page {
  color: #dd2b2b;
}

/* 筛选区域样式 */
.filter-section {
padding: 20px;
background-color: #fff;
}

.filter-row {
display: flex;
align-items: center;
margin-bottom: 15px;
}

.filter-label {
width: 100px;
font-weight: bold;
color: #333;
}

.filter-options {
display: flex;
flex-wrap: wrap;
gap: 10px;
}

.option {
padding: 5px 15px;
cursor: pointer;
color: #000000;
background-color: #fff;
border-radius: 3px;
transition: all 0.1s ease;
}

/* 页面加载时"全部"的默认激活状态 */
.option.default-active {
color: #fff;
background-color: #e31436;
}

/* 点击后的激活状态 */
.option.active {
color: #fff;
background-color: #e31436;
}

/* 悬停效果 */
.option:hover {
color: #fff;
background-color: #e31436;
}

/* 队伍列表样式 */
.team-list {
margin-top: 20px;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 20px;
padding: 20px;
}

.team-card {
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 12px rgba(0,0,0,0.1);
overflow: hidden;
transition: transform 0.3s ease;
display: flex;
padding: 20px;
cursor: pointer;
}

.team-card:hover {
transform: translateY(-5px);
}

.team-logo {
width: 100px;
height: 100px;
margin-right: 20px;
}

.team-logo img {
width: 100%;
height: 100%;
object-fit: cover;
border-radius: 50%;
}

.team-info {
flex: 1;
}

.team-info h3 {
margin: 0 0 10px 0;
color: #333;
}

.team-desc {
color: #666;
font-size: 14px;
margin-bottom: 10px;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}

.team-meta {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 10px;
font-size: 13px;
color: #666;
}

.team-stats {
display: flex;
justify-content: space-between;
border-top: 1px solid #eee;
padding-top: 10px;
}

.stat-item {
text-align: center;
}

.stat-value {
font-size: 18px;
font-weight: bold;
color: #e31436;
}

.stat-label {
font-size: 12px;
color: #999;
}
</style>